<?php

class Template{

	public	$tplData;
	
	protected $path = "./";
	protected $used_path = "./";
	
	private $template;
	private $print_var;
	public function __construct(){
			$db = new DB("settings");
			$db->select();
			$db->nextRecord();
		
			$this->setPath("themes/".$db->theme."/");
	}

	public function setPath($path){
		if (is_dir($path) && is_readable($path)){
			$this->path = $path;
		}else{
			trigger_error("Template path: <b>".$path."</b>, is not a valid directory", E_USER_ERROR);
		}
	}

	public function build($template){
		$template = $template.".php";
		$this->print_var = $print_var;
		$full_path = $this->path . $template;
		$this->template = $template;
		if ( file_exists($full_path) ){
			include $full_path;
			$this->used_path = $full_path;
		}else{
			trigger_error("Template file ".$full_path." does not exist", E_USER_ERROR);
		}
	
	}
	
	public function __set($var, $val){
		$this->tplData[$var] = $val;
	}
	
	public function __get($var){
		if (isset($this->tplData[$var])){
			return $this->tplData[$var];
		}else{
			if ( $this->print_var ){
				return "\$" . $var;
			}else{
				trigger_error("Template data (<b>".$var."</b>) does not exist, in <b>" . $this->used_path . "</b>", E_USER_ERROR);
			}
				
		}
	
	}

	public function dataIsset($key){
		if ( isset($this->tplData[$key]) ){
			return true;
		}
		return false;
	}

	public function __isset($variableName){
		return $this->dataIsset($variableName);
	}
	
}

?>
